package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.Permission;
import com.xrit.sunnyday.service.PermissionService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 查询所有
     */

    /**
     * 通过条件分页查询
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = permissionService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, page);
    }

    /**
     * 添加权限
     */
    @PostMapping("/add")
    public Result add(@RequestBody Permission permission) {
        permissionService.add(permission);
        //返回
        return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
    }

    /**
     * 通过id查询权限
     */
    @GetMapping("/findById")
    public Result findById(int id) {
        Permission permission = permissionService.findById(id);
        return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permission);
    }

    /**
     * 修改权限
     */
    @PostMapping("update")
    public Result update(@RequestBody Permission permission){
        permissionService.update(permission);
        return new Result(true,MessageConstant.EDIT_PERMISSION_SUCCESS);
    }

    /**
     * 删除权限
     */
    @DeleteMapping("/delete")
    public Result delete(int id){
        permissionService.delete(id);
        return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
    }

    /**
     * 查询全部
     */
    @PostMapping("/findAll.do")
    public Result findAll(){
        List<Permission> permissionList =permissionService.findAll();
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS,permissionList);
    }
}
